﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace DentScan2.WebApp
{
    public partial class Users : Classes.BaseForm
    {

        public Guid? CurrentUserId
        {
            get
            {
                return ViewState["CurrentUserId"] == null ? null : ViewState["CurrentUserId"] as Guid?;
            }
            set
            {
                ViewState["CurrentUserId"] = value;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            this.CheckLogged();
            this.CheckLevel(1);
            if (!IsPostBack)
            {
                Search();
                InitDDL();                
                
            }
        }

        private void InitDDL()
        {
            ddlLevel.DataTextField = "LevelName";
            ddlLevel.DataValueField = "LevelId";
            ddlLevel.DataSource = dl.Levels.ToArray();
            ddlLevel.DataBind();
        }
        private void Search()
        {
            rUsers.DataSource = dl.Users.OrderBy(user => user.FullName);
            rUsers.DataBind();
        }

        private void Save()
        {
            Page.Validate("vgUser");
            if (!Page.IsValid)
                return;

            string pass = Tools.Tools.GenPass();

            if (this.CurrentUserId == null)
            {
                DB.User user = new DB.User()
                {
                    UserId = Guid.NewGuid(),
                    UserName = tbUserName.Text,
                    FullName = tbFullName.Text,
                    Password = Tools.Tools.GetMd5Hash(pass),
                    LevelId = int.Parse(ddlLevel.SelectedValue),
                    LastEditDate = DateTime.UtcNow
                };
                dl.Users.InsertOnSubmit(user);
                dl.SubmitChanges();

                this.Alert("Password: " + pass);


            }
            else
            {
                DB.User user = dl.Users.Where(item => item.UserId == this.CurrentUserId.Value).First();
                user.FullName = tbFullName.Text;
                user.LastEditDate = DateTime.UtcNow;
                dl.SubmitChanges();

            }
            Search();
            mvUsers.SetActiveView(vUsers);
        }



        private void Cancel()
        {
            mvUsers.SetActiveView(vUsers);
        }

        private void Edit(string userId)
        {
            this.CurrentUserId = Guid.Parse(userId);
            var user = dl.Users.Where(item => item.UserId == this.CurrentUserId.Value).First();
            tbUserName.Enabled = false;
            ddlLevel.Enabled = false;
            tbFullName.Text = user.FullName;
            tbUserName.Text = user.UserName;
            ddlLevel.SelectedValue = user.LevelId.ToString();
            mvUsers.SetActiveView(vEdit);
        }

        private void New()
        {
            this.CurrentUserId = null;
            tbUserName.Enabled = true;
            ddlLevel.Enabled = true;
            tbFullName.Text = "";
            tbUserName.Text = "";
            ddlLevel.SelectedValue = "2";
            mvUsers.SetActiveView(vEdit);
        }

        private void PassChange(string userId)
        {
            Guid uid = Guid.Parse(userId);
            string pass = Tools.Tools.GenPass();
            DB.User user = dl.Users.Where(item => item.UserId == uid).First();
            user.Password = Tools.Tools.GetMd5Hash(pass);
            dl.SubmitChanges();

            this.Alert("Password:" + pass);
        }

        protected void rUsers_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "DEACTIVATE":
                    DB.User user = dl.Users.Where(item => item.UserId == Guid.Parse(e.CommandArgument.ToString())).First();
                    user.IsDel = !user.IsDel;
                    dl.SubmitChanges();
                    Search();
                    break;
                case "EDIT": this.Edit(e.CommandArgument.ToString()); break;
                case "PASS": this.PassChange(e.CommandArgument.ToString()); break;
            }
        }

        protected void cvUserName_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = dl.Users.Where(user => user.UserName == tbUserName.Text).FirstOrDefault() == null || this.CurrentUserId != null;
        }

        protected void bNew_Click(object sender, EventArgs e)
        {
            this.New();
        }

        protected void bSave_Click(object sender, EventArgs e)
        {
            this.Save();
        }

        protected void bCancel_Click(object sender, EventArgs e)
        {
            this.Cancel();
        }


    }
}